using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System;
	using System.Web.Mvc;
	using System.Web.Security;
	using System.Collections.Generic;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using TemplateWeb.Models;
	using System.IO;
	using System.Web;
    using TemplateWeb.ShoppingCartModule;
	#endregion
	public abstract class BaseController : Controller
	{
		protected ThongTinCongTyRepository _repThongTinCongTy = new ThongTinCongTyRepository();
		protected TinTucRepository _repTinTuc = new TinTucRepository();
		protected DienThoaiRepository _repDienThoai = new DienThoaiRepository();
        protected SanPhamRepository _repSanPham = new SanPhamRepository();
        protected PhuKienRepository _repPhuKien = new PhuKienRepository();
		protected PhanHoiRepository _repPhanHoi = new PhanHoiRepository();
        protected NhaSanXuatRepository _repNhaSanXuat = new NhaSanXuatRepository();
		protected HopThuRepository _repHopThu = new HopThuRepository();
		protected NguoiDungRepository _repNguoiDung = new NguoiDungRepository();
        protected GioHangRepository _repCart = new GioHangRepository();
        protected DonDatHangRepository _repDonDatHang = new DonDatHangRepository();
        protected ChiTietDonDatHangRepository _repChiTietDonDatHang = new ChiTietDonDatHangRepository();

		protected string RenderPartialViewToString()
		{
			return RenderPartialViewToString(null, null);
		}

		protected string RenderPartialViewToString(string viewName)
		{
			return RenderPartialViewToString(viewName, null);
		}

		protected string RenderPartialViewToString(string viewName, object model)
		{
			if (string.IsNullOrEmpty(viewName))
				viewName = ControllerContext.RouteData.GetRequiredString("action");
			ViewData.Model = model;
			using (StringWriter sw = new StringWriter())
			{
				ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
				ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
				viewResult.View.Render(viewContext, sw);
				return sw.GetStringBuilder().ToString();
			}
		}


		public GroupViewModel CreateViewModel(DataTransferViewModel dataTransfer)
		{
			GroupViewModel viewModel = new GroupViewModel();
			if (dataTransfer.EnumViewModelType.ToString().Contains("ADMIN"))
			{
                viewModel.NhaSanXuatModel = RetrieveNhaSanXuatViewModel(dataTransfer);
            }
            else if (dataTransfer.EnumViewModelType.ToString().Contains("HOME"))
            {
                viewModel.ThongTinCongTyModel = RetrieveThongTinCongTyViewModel(dataTransfer);
                viewModel.NhaSanXuatModel = RetrieveNhaSanXuatViewModel(dataTransfer);

                // Add it to the shopping cart 
                var cart = ShoppingCart.GetCart(this.HttpContext);
                // Set up shoppingCart ViewModel
                viewModel.ShoppingCartModel = new GioHangViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartTotal = cart.GetTotal()
                };

                if (User.Identity.IsAuthenticated)
                {
                    dataTransfer.UserName = User.Identity.Name;
                    viewModel.UsersModel = RetrieveDetailOfNguoiDungByUserNameViewModel(dataTransfer);
                }
            }
			switch (dataTransfer.EnumViewModelType)
			{
				case EnumViewModel.ADMIN_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY:
                    viewModel.ThongTinCongTyModel = RetrieveDetailOfThongTinCongTyViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_TINTUC:
                    viewModel.TinTucModel = RetrieveDetailOfTinTucViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_DIENTHOAI:
                    viewModel.DienThoaiModel = RetrieveDetailOfDienThoaiViewModel(dataTransfer);
					break;
                case EnumViewModel.ADMIN_DETAILOF_PHUKIEN:
                    viewModel.PhuKienModel = RetrieveDetailOfPhuKienViewModel(dataTransfer);
                    break;
				case EnumViewModel.ADMIN_DETAILOF_PHANHOI:
                    viewModel.PhanHoiModel = RetrieveDetailOfPhanHoiViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_HOPTHU:
					break;
				case EnumViewModel.ADMIN_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveNguoiDungViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_THONGTINCONGTY:
                    viewModel.ThongTinCongTyModel = RetrieveThongTinCongTyViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_TINTUC:
                    viewModel.TinTucModel = RetrieveTinTucViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DIENTHOAI:
                    viewModel.DienThoaiModel = RetrieveDienThoaiViewModel(dataTransfer);
                    viewModel.SelectedNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
					break;
                case EnumViewModel.ADMIN_PHUKIEN:
                    viewModel.PhuKienModel = RetrievePhuKienViewModel(dataTransfer);
                    break;
				case EnumViewModel.ADMIN_PHANHOI:
                    viewModel.PhanHoiModel = RetrievePhanHoiViewModel(dataTransfer);
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_HOPTHU:
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
					break;
                case EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE:
                    viewModel.DonDatHangModel = RetrieveOrdersRevenueViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_ORDERDETAIL:
                    viewModel.DonDatHangModel = RetrieveOrdersRevenueViewModel(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_TIMKIEM_DIENTHOAI:
                    viewModel.DienThoaiModel = RetrieveTimKiemDienThoaiViewModel(dataTransfer);
                    viewModel.SelectedNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
                    break;
				case EnumViewModel.HOME_REGISTER:
					break;
				case EnumViewModel.HOME_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungByUserNameViewModel(dataTransfer);
					break;
                case EnumViewModel.HOME_INDEX:
                    viewModel.DienThoaiModel = RetrieveDienThoaiViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETDIENTHOAI:
                    viewModel.DienThoaiModel = RetrieveDetailOfDienThoaiViewModel(dataTransfer);
                    viewModel.AllDienThoaiModel = RetrieveAllDienThoaiViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETPHUKIEN:
                    viewModel.PhuKienModel = RetrieveDetailOfPhuKienViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETGOCCHIASE:
                    break;
                case EnumViewModel.HOME_DIENTHOAI:
                    viewModel.DienThoaiModel = RetrieveDienThoaiViewModel(dataTransfer);
                    viewModel.DetaifOfNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
                    viewModel.SelectedNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_PHUKIEN:
                    viewModel.PhuKienModel = RetrievePhuKienViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_TINTUC:
                    viewModel.TinTucModel = RetrieveTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETTINTUC:
                    viewModel.TinTucModel = RetrieveDetailOfTinTucViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_THEMCHIASE:
                    break;
                case EnumViewModel.HOME_ORDERHISTORY:
                    viewModel.DonDatHangModel = RetrieveDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_ORDERHISTORY_ORDERDETAIL:
                    viewModel.DonDatHangModel = RetrieveDonDatHangViewModel(dataTransfer);
                    viewModel.SelectedDonDatHangModel = RetrieveDetailOfDonDatHangViewModel(dataTransfer);
                    viewModel.ChiTietDonDatHangModel = RetrieveChiTietDonDatHangViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_TIMKIEM_SELECTNSX:
                    viewModel.SelectedNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
                    viewModel.DienThoaiModel = RetrieveDienThoaiViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_KETQUATIMKIEM:
                    viewModel.SelectedNhaSanXuatModel = RetrieveDetailOfNhaSanXuatViewModel(dataTransfer);
                    viewModel.DienThoaiModel = SearchDienThoai(dataTransfer);
                    break;
			}
			return viewModel;
		}

		public bool UploadFile(HttpPostedFileBase file, string path, ref string refName)
		{
			try
			{
				if (!System.IO.File.Exists(Server.MapPath(path + file.FileName)))
				{
					FileInfo f = new FileInfo(file.FileName);
					long ngay = DateTime.Now.ToBinary();
					refName = ngay + f.Extension;
					string fullPath = path + refName;
					if (file == null)
					return false;
					byte[] buffer = new byte[file.ContentLength];
					file.InputStream.Read(buffer, 0, file.ContentLength);
					FileStream newFile = new FileStream(Server.MapPath(fullPath), FileMode.Create, FileAccess.Write);
					newFile.Write(buffer, 0, file.ContentLength);
					newFile.Close();
				}
				else
				{
					refName = file.FileName;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		public void DeleteFile(string filePath)
		{
			FileInfo file = new FileInfo(filePath);
			if (file.Exists)
			{
				file.Delete();
			}
		}

		public bool SendingMail(string subject, string emailContent, string to)
		{
			bool result = false;
			try
			{
                var thongtincongty = RetrieveThongTinCongTyViewModel(new DataTransferViewModel());
                if (thongtincongty.GetModel.LstObjModel.Count > 0)
                {
                    WebConfiguration.EmailLienHe = thongtincongty.GetModel.LstObjModel[0].EmailCongTy;
                    WebConfiguration.PasswordEmailLienHe = Encryption.Decrypt(thongtincongty.GetModel.LstObjModel[0].MatKhauEmailCongTy);
                    emailContent = emailContent.Replace("[EmailLienHe]", WebConfiguration.EmailLienHe);

                    System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
                    System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
                    mailclient.Host = WebConfiguration.MailServer;
                    mailclient.EnableSsl = true;
                    mailclient.UseDefaultCredentials = true;
                    mailclient.Credentials = auth;
                    System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
                    mm.Subject = subject;
                    mm.IsBodyHtml = true;
                    mm.Body = emailContent;
                    mailclient.Send(mm);
                    result = true;
                }
			}
			catch
			{
				result = false;
			}
			return result;
		}

        public DienThoaiViewModel RetrieveTimKiemDienThoaiViewModel(DataTransferViewModel dataTransfer)
        {
            List<DienThoai> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDNhaSanXuat != -1)
            {
                lst = _repDienThoai.Search(dataTransfer.IDNhaSanXuat.ToString(), string.Empty, string.Empty, dataTransfer.TimKiem_TenSanPham);
                totalitem = lst.Count;
            }
            else
            {
                lst = _repDienThoai.SelectAll();
                totalitem = lst.Count;
            }
            return new DienThoaiViewModel
            {
                GetModel = new GetDienThoaiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDienThoaiViewModel
                {
                },
                EditModel = new EditDienThoaiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DienThoaiReferKeys()
                {
                    IDNhaSanXuat = dataTransfer.IDNhaSanXuat,
                },
                InfoText = dataTransfer.InfoText
            };
        }


        public DienThoaiViewModel SearchDienThoai(DataTransferViewModel dataTransfer)
        {
            var lst = _repDienThoai.Search(dataTransfer.TimKiem_IDNhaSanXuat, dataTransfer.TimKiem_GiaTu, dataTransfer.TimKiem_GiaDen, dataTransfer.TimKiem_TenSanPham);
            var totalitem = lst.Count;

            return new DienThoaiViewModel
            {
                GetModel = new GetDienThoaiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDienThoaiViewModel
                {
                },
                EditModel = new EditDienThoaiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DienThoaiReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhuKienViewModel RetrievePhuKienViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repPhuKien.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumPhuKienPerPage);
            var totalitem = _repPhuKien.GetTotalItem();

            return new PhuKienViewModel
            {
                GetModel = new GetPhuKienViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhuKienViewModel
                {
                },
                EditModel = new EditPhuKienViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhuKienReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DonDatHangViewModel RetrieveOrdersRevenueViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = new List<DonDatHang>();
            var lstOrder = _repDonDatHang.SelectAll().OrderByDescending(n => n.NgayDatHang);

            foreach (var item in lstOrder)
            {
                if (dataTransfer.From.Date <= item.NgayDatHang.Value.Date && item.NgayDatHang.Value.Date <= dataTransfer.To.Date)
                {
                    lst.Add(item);
                }
            }
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                    From = dataTransfer.From,
                    To = dataTransfer.To
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public ChiTietDonDatHangViewModel RetrieveChiTietDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            List<ChiTietDonDatHang> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDDonDatHang != -1)
            {
                lst = _repChiTietDonDatHang.SelectByIDDonDatHang(dataTransfer.IDDonDatHang);
                totalitem = lst.Count;
            }
            return new ChiTietDonDatHangViewModel
            {
                GetModel = new GetChiTietDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddChiTietDonDatHangViewModel
                {
                },
                EditModel = new EditChiTietDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new ChiTietDonDatHangReferKeys()
                {
                    IDDonDatHang = dataTransfer.IDDonDatHang,
                    IDSanPham = dataTransfer.IDSanPham,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public DonDatHangViewModel RetrieveDetailOfDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repDonDatHang.RetrieveByID(dataTransfer.IDDonDatHang);
            var totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                }
            };
        }

        public DonDatHangViewModel RetrieveDonDatHangViewModel(DataTransferViewModel dataTransfer)
        {
            List<DonDatHang> lst = null;
            int totalitem = -1;

            if (dataTransfer.GuidID.ToString() != "11112222-1111-1111-1111-111122223333")
            {
                lst = _repDonDatHang.SelectByIDUsers(dataTransfer.GuidID);
            }
            else
            {
                lst = _repDonDatHang.SelectAll();
            }

            totalitem = lst.Count;

            return new DonDatHangViewModel
            {
                GetModel = new GetDonDatHangViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDonDatHangViewModel
                {
                },
                EditModel = new EditDonDatHangViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DonDatHangReferKeys()
                {
                    IDUser = dataTransfer.GuidID
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public ThongTinCongTyViewModel RetrieveThongTinCongTyViewModel(DataTransferViewModel dataTransfer)
		{
			return new ThongTinCongTyViewModel
			{
				GetModel = new GetThongTinCongTyViewModel
				{
					LstObjModel = _repThongTinCongTy.SelectAll(),
					TotalItem = _repThongTinCongTy.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddThongTinCongTyViewModel
				{
				},
				EditModel = new EditThongTinCongTyViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public TinTucViewModel RetrieveTinTucNewestViewModel(DataTransferViewModel dataTransfer)
        {
            List<TinTuc> lst = _repTinTuc.SelectNewest();
            int totalitem = lst.Count;

            return new TinTucViewModel
            {
                GetModel = new GetTinTucViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddTinTucViewModel
                {
                },
                EditModel = new EditTinTucViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new TinTucReferKeys()
                {
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public TinTucViewModel RetrieveTinTucViewModel(DataTransferViewModel dataTransfer)
		{
			List<TinTuc> lst = null;
			int totalitem = -1;
            
            lst = _repTinTuc.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumTinTucPerPage);
            totalitem = _repTinTuc.GetTotalItem();

			return new TinTucViewModel
			{
				GetModel = new GetTinTucViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddTinTucViewModel
				{
				},
				EditModel = new EditTinTucViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new TinTucReferKeys()
				{
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public DienThoaiViewModel RetrieveDienThoaiBestSellerViewModel(DataTransferViewModel dataTransfer)
        {
            List<DienThoai> lst = _repDienThoai.SelectBestSeller();
            int totalitem = lst.Count;

            return new DienThoaiViewModel
            {
                GetModel = new GetDienThoaiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDienThoaiViewModel
                {
                },
                EditModel = new EditDienThoaiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DienThoaiReferKeys()
                {
                    IDNhaSanXuat = dataTransfer.IDNhaSanXuat,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public DienThoaiViewModel RetrieveDienThoaiViewModel(DataTransferViewModel dataTransfer)
		{
			List<DienThoai> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDNhaSanXuat != -1)
			{
                lst = _repDienThoai.SelectByIDNhaSanXuatPaging(dataTransfer.IDNhaSanXuat, dataTransfer.CurrentPage, WebConfiguration.NumDienThoaiPerPage);
                totalitem = _repDienThoai.GetTotalItemByIDNhaSanXuat(dataTransfer.IDNhaSanXuat);
			}
			else
			{
				lst = _repDienThoai.SelectAll();
				totalitem = lst.Count;
			}
			return new DienThoaiViewModel
			{
				GetModel = new GetDienThoaiViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddDienThoaiViewModel
				{
				},
				EditModel = new EditDienThoaiViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new DienThoaiReferKeys()
				{
					IDNhaSanXuat = dataTransfer.IDNhaSanXuat,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public DienThoaiViewModel RetrieveAllDienThoaiViewModel(DataTransferViewModel dataTransfer)
        {
            List<DienThoai> lst = _repDienThoai.SelectAll();
            int totalitem = lst.Count;

            return new DienThoaiViewModel
            {
                GetModel = new GetDienThoaiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDienThoaiViewModel
                {
                },
                EditModel = new EditDienThoaiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DienThoaiReferKeys()
                {
                    IDNhaSanXuat = dataTransfer.IDNhaSanXuat,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public PhanHoiViewModel RetrievePhanHoiViewModel(DataTransferViewModel dataTransfer)
		{
			List<PhanHoi> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDHopThu != -1)
			{
				lst = _repPhanHoi.SelectByIDHopThu(dataTransfer.IDHopThu);
				totalitem = _repPhanHoi.GetTotalItemByIDHopThu(dataTransfer.IDHopThu);
			}
			else
			{
				lst = _repPhanHoi.SelectAll();
				totalitem = lst.Count;
			}
			return new PhanHoiViewModel
			{
				GetModel = new GetPhanHoiViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddPhanHoiViewModel
				{
				},
				EditModel = new EditPhanHoiViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new PhanHoiReferKeys()
				{
					IDHopThu = dataTransfer.IDHopThu,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public PhanHoiViewModel RetrieveDetailOfPhanHoiViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhanHoi> lst = null;
            int totalitem = -1;

            lst = _repPhanHoi.RetrieveByID(dataTransfer.IntID);
            totalitem = lst.Count;

            return new PhanHoiViewModel
            {
                GetModel = new GetPhanHoiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhanHoiViewModel
                {
                },
                EditModel = new EditPhanHoiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhanHoiReferKeys()
                {
                    IDHopThu = dataTransfer.IDHopThu,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public NhaSanXuatViewModel RetrieveDetailOfNhaSanXuatViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repNhaSanXuat.RetrieveByID(dataTransfer.IDNhaSanXuat);
            return new NhaSanXuatViewModel
            {
                GetModel = new GetNhaSanXuatViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = 1
                },
                AddModel = new AddNhaSanXuatViewModel
                {
                },
                EditModel = new EditNhaSanXuatViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public NhaSanXuatViewModel RetrieveNhaSanXuatViewModel(DataTransferViewModel dataTransfer)
		{
			return new NhaSanXuatViewModel
			{
				GetModel = new GetNhaSanXuatViewModel
				{
					LstObjModel = _repNhaSanXuat.SelectAll(),
					TotalItem = _repNhaSanXuat.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddNhaSanXuatViewModel
				{
				},
				EditModel = new EditNhaSanXuatViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public HopThuViewModel RetrieveHopThuViewModel(DataTransferViewModel dataTransfer)
		{
			return new HopThuViewModel
			{
				GetModel = new GetHopThuViewModel
				{
					LstObjModel = _repHopThu.SelectAll(),
					TotalItem = _repHopThu.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddHopThuViewModel
				{
				},
				EditModel = new EditHopThuViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public NguoiDungViewModel RetrieveNguoiDungViewModel(DataTransferViewModel dataTransfer)
		{
			return new NguoiDungViewModel
			{
				GetModel = new GetNguoiDungViewModel
				{
					LstObjModel = _repNguoiDung.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumNguoiDungPerPage, dataTransfer.Role),
					TotalItem = _repNguoiDung.GetTotalItem(dataTransfer.Role),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddNguoiDungViewModel
				{
				},
				EditModel = new EditNguoiDungViewModel
				{
					ID = dataTransfer.GuidID,
				}
			};
		}

		public NguoiDungViewModel RetrieveDetailOfNguoiDungViewModel(DataTransferViewModel dataTransfer)
		{
			return new NguoiDungViewModel
			{
				GetModel = new GetNguoiDungViewModel
				{
					LstObjModel = _repNguoiDung.RetrieveByID(dataTransfer.GuidID)
				},
				EditModel = new EditNguoiDungViewModel
				{
					ID = dataTransfer.GuidID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
        public NguoiDungViewModel RetrieveDetailOfNguoiDungByUserNameViewModel(DataTransferViewModel dataTransfer)
        {
            return new NguoiDungViewModel
            {
                GetModel = new GetNguoiDungViewModel
                {
                    LstObjModel = new List<NguoiDungInfo>
                    {
                        _repNguoiDung.SelectByUserName(dataTransfer.UserName)
                    }
                },
                EditModel = new EditNguoiDungViewModel
                {
                    ID = dataTransfer.GuidID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
		public ThongTinCongTyViewModel RetrieveDetailOfThongTinCongTyViewModel(DataTransferViewModel dataTransfer)
		{
			return new ThongTinCongTyViewModel
			{
				GetModel = new GetThongTinCongTyViewModel
				{
					LstObjModel = _repThongTinCongTy.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditThongTinCongTyViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public TinTucViewModel RetrieveDetailOfTinTucViewModel(DataTransferViewModel dataTransfer)
		{
			return new TinTucViewModel
			{
				GetModel = new GetTinTucViewModel
				{
					LstObjModel = _repTinTuc.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditTinTucViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public DienThoaiViewModel RetrieveDetailOfDienThoaiViewModel(DataTransferViewModel dataTransfer)
		{
			return new DienThoaiViewModel
			{
				GetModel = new GetDienThoaiViewModel
				{
					LstObjModel = _repDienThoai.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditDienThoaiViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
        public PhuKienViewModel RetrieveDetailOfPhuKienViewModel(DataTransferViewModel dataTransfer)
        {
            return new PhuKienViewModel
            {
                GetModel = new GetPhuKienViewModel
                {
                    LstObjModel = _repPhuKien.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditPhuKienViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

	}
}